package com.example.graphic.controller;

import com.example.graphic.entity.User;
import com.example.graphic.model.dto.UserLoginDTO;
import com.example.graphic.model.dto.UserRegisterDTO;
import com.example.graphic.model.vo.BaseResultVO;
import com.example.graphic.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 用户接口示例
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/list")
    public List<User> users() {
        return userService.list();
    }

    @PostMapping("/create")
    public BaseResultVO<Boolean> create(@RequestBody User user) {
        boolean ok = userService.save(user);
        return BaseResultVO.success(ok);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResultVO<Boolean> register(@RequestBody UserRegisterDTO dto) {

            boolean ok = userService.register(dto.getUsername(), dto.getPassword(), dto.getEmail());
            if(ok) {
                return BaseResultVO.success(ok);
            }else{
                return BaseResultVO.fail("注册失败");
            }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResultVO<User> login(@RequestBody  UserLoginDTO dto) {
        try {
            User user = userService.login(dto.getUsername(), dto.getPassword());
            return BaseResultVO.success(user);
        } catch (Exception e) {
            return BaseResultVO.fail(e.getMessage());
        }
    }
} 